$(function () {
    vm.categories();

    $('#datetimeStart').datetimepicker({
        format: 'yyyy-mm-dd',  //显示格式可为yyyymm/yyyy-mm-dd/yyyy.mm.dd
        autoclose: true,
        minView : 'month',
        initialDate: new Date(),	//初始化日期.默认new Date()当前日期
        forceParse: false,  	//当输入非格式化日期时，强制格式化。默认true
        language: 'zh-CN' //语言
    })


    $('#datetimeEnd').datetimepicker({
        format: 'yyyy-mm-dd',  //显示格式可为yyyymm/yyyy-mm-dd/yyyy.mm.dd
        autoclose: true,
        minView : 'month',
        initialDate: new Date(),	//初始化日期.默认new Date()当前日期
        forceParse: false,  	//当输入非格式化日期时，强制格式化。默认true
        language: 'zh-CN' //语言
    })

    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/purchase/list',
        datatype: "json",
        colModel: [
            { label: '创建时间', name: 'createTime', index: 'create_time', width: 110 },
            { label: '进货数量', name: 'number', index: 'number', width: 50 },
            { label: '进货价', name: 'price', index: 'price', width: 50 },
            { label: '建议售价', name: 'suggestPrice', index: 'suggest_price', width: 50 },
            { label: '产品名称', name: 'name', index: 'name', width: 60 },
            { label: '产品编号', name: 'code', index: 'code', width: 60 },
            { label: '供应商', name: 'supplierName', index: 'supplier_name', width: 60 },
            { label: '产品分类', name: 'cateName', index: 'cate_name', width: 50 },
            { label: '材质', name: 'material', index: 'material', width: 50 },
            { label: '型号', name: 'model', index: 'model', width: 50 },
			{ label: '备注', name: 'remark', index: 'remark', width: 100 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		purchase: {},
        categoryList:[],
        searcher:{
            productName:null,
            code:null,
            shopName:null,
            category:null,
            datetimeStart:null,
            datetimeEnd:null
        }
	},
	methods: {
		query: function () {
            vm.searcher.datetimeStart = $("#datetimeStart").val();
            vm.searcher.datetimeEnd = $("#datetimeEnd").val();
            vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.purchase = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.purchase.id == null ? "sys/purchase/save" : "sys/purchase/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.purchase),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/purchase/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/purchase/info/"+id, function(r){
                vm.purchase = r.purchase;
            });
		},
        categories: function () {
            $.get(baseURL + "sys/productcategory/list", function(r){
                vm.categoryList = r.page.list;
            });
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    "productName":vm.searcher.productName,
                    "code":vm.searcher.code,
                    "shopName":vm.searcher.shopName,
                    "category":vm.searcher.category,
                    "datetimeStart":vm.searcher.datetimeStart,
                    "datetimeEnd":vm.searcher.datetimeEnd
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});